﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine;

namespace TorchesTombs
{
    public class DoorSprite : MapObjectSprite
    {
        //constructor
        public DoorSprite(Door door)
            : base(door)
        {
            //adjust bounds based on orientation of door
            RectangleF r = Shape;
            if (door.Orientation == SCGOrientation.Horizontal)
                r.Width += 64;
            else
                r.Height += 64;
            ClickShape = r;
        }

        //access the door
        public Door Door
        {
            get
            {
                return MapObject as Door;
            }
        }

        //render both doors
        public override void Render(Graphics g)
        {
            
            int dx = Door.X2 * 64 + 32 - WidthHalf - Surface.OffsetX;
            int dy = Door.Y2 * 64 + 32 - HeightHalf - Surface.OffsetY;

            //render
            if (Door.Orientation == SCGOrientation.Horizontal)
            {
                if (Door.IsOpen)
                {
                    if (_squeezed == null)
                    {
                        _squeezed = new Bitmap(16, 64);
                        Graphics gs = Graphics.FromImage(_squeezed);
                        using (gs)
                        {
                            gs.DrawImage(Bitmap, 0, 0, 16, 64);
                        }
                    }
                    g.DrawImage(_squeezed, dx - 64, dy);
                    g.DrawImage(_squeezed, dx + 64, dy, -16, 64);
                }
                else
                {
                    base.Render(g);
                    g.DrawImage(Bitmap, dx + 64, dy, -64, 64);
                }
            }
            else
            {
                if (_rotated == null)
                {
                    _rotated = new Bitmap(Bitmap);
                    _rotated.RotateFlip(RotateFlipType.Rotate90FlipY);
                }
                if (Door.IsOpen)
                {
                    if (_squeezed == null)
                    {
                        _squeezed = new Bitmap(16, 64);
                        Graphics gs = Graphics.FromImage(_squeezed);
                        using (gs)
                        {
                            gs.DrawImage(_rotated, 0, 0, 16, 64);
                        }
                    }
                    g.DrawImage(_squeezed, X - WidthHalf - Surface.OffsetX, Y - HeightHalf - Surface.OffsetY, 64, 16);
                    g.DrawImage(_squeezed, dx, dy + 64, 64, -16);
                }
                else
                {
                    g.DrawImage(_rotated, X - WidthHalf - Surface.OffsetX, Y - HeightHalf - Surface.OffsetY + 64, 64, 64);
                    g.DrawImage(_rotated, dx, dy, 64, -64);
                }
            }
        }

        //private members
        private Bitmap _rotated = null;
        private Bitmap _squeezed = null;
    }
}
